﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;

namespace Pahan.McuFusion.UI.Workspaces
{
    /*public partial class Workspace : ICustomTypeDescriptor
    {
        #region Implementation of ICustomTypeDescriptor

        /// <summary>
        /// Returns a collection of custom attributes for this instance of a component.
        /// </summary>
        /// <returns>
        /// An <see cref="TValue:System.ComponentModel.AttributeCollection"></see> containing the attributes for this object.
        /// </returns>
        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }

        /// <summary>
        /// Returns the class name of this instance of a component.
        /// </summary>
        /// <returns>
        /// The class name of the object, or null if the class does not have a name.
        /// </returns>
        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }

        /// <summary>
        /// Returns the name of this instance of a component.
        /// </summary>
        /// <returns>
        /// The name of the object, or null if the object does not have a name.
        /// </returns>
        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }

        /// <summary>
        /// Returns a type converter for this instance of a component.
        /// </summary>
        /// <returns>
        /// A <see cref="TValue:System.ComponentModel.TypeConverter"></see> that is the converter for this object, or null if there is no <see cref="TValue:System.ComponentModel.TypeConverter"></see> for this object.
        /// </returns>
        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }

        /// <summary>
        /// Returns the default event for this instance of a component.
        /// </summary>
        /// <returns>
        /// An <see cref="TValue:System.ComponentModel.EventDescriptor"></see> that represents the default event for this object, or null if this object does not have events.
        /// </returns>
        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }

        /// <summary>
        /// Returns the default property for this instance of a component.
        /// </summary>
        /// <returns>
        /// A <see cref="TValue:System.ComponentModel.PropertyDescriptor"></see> that represents the default property for this object, or null if this object does not have properties.
        /// </returns>
        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }

        /// <summary>
        /// Returns an editor of the specified type for this instance of a component.
        /// </summary>
        /// <returns>
        /// An <see cref="TValue:System.Object"></see> of the specified type that is the editor for this object, or null if the editor cannot be found.
        /// </returns>
        /// <param name="editorBaseType">A <see cref="TValue:System.Type"></see> that represents the editor for this object. </param>
        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }

        /// <summary>
        /// Returns the events for this instance of a component.
        /// </summary>
        /// <returns>
        /// An <see cref="TValue:System.ComponentModel.EventDescriptorCollection"></see> that represents the events for this component instance.
        /// </returns>
        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }

        /// <summary>
        /// Returns the events for this instance of a component using the specified attribute array as a filter.
        /// </summary>
        /// <returns>
        /// An <see cref="TValue:System.ComponentModel.EventDescriptorCollection"></see> that represents the filtered events for this component instance.
        /// </returns>
        /// <param name="attributes">An array of type <see cref="TValue:System.Attribute"></see> that is used as a filter. </param>
        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }

        /// <summary>
        /// Returns the properties for this instance of a component.
        /// </summary>
        /// <returns>
        /// A <see cref="TValue:System.ComponentModel.PropertyDescriptorCollection"></see> that represents the properties for this component instance.
        /// </returns>
        public PropertyDescriptorCollection GetProperties()
        {
            var properties = TypeDescriptor.GetProperties(programmer, true);
            return properties;
        }

        /// <summary>
        /// Returns the properties for this instance of a component using the attribute array as a filter.
        /// </summary>
        /// <returns>
        /// A <see cref="TValue:System.ComponentModel.PropertyDescriptorCollection"></see> that represents the filtered properties for this component instance.
        /// </returns>
        /// <param name="attributes">An array of type <see cref="TValue:System.Attribute"></see> that is used as a filter. </param>
        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection properties = new PropertyDescriptorCollection(null);
            AddProperties(properties, DeviceInfo, attributes, "Device");
            AddProperties(properties, programmer, attributes, "Programmer");
            AddProperties(properties, adapter, attributes, "Adapter");
            AddProperties(properties, hwInterface, attributes, "Interface");
            return properties;
        }

        /// <summary>
        /// Returns an object that contains the property described by the specified property descriptor.
        /// </summary>
        /// <returns>
        /// An <see cref="TValue:System.Object"></see> that represents the owner of the specified property.
        /// </returns>
        /// <param name="pd">A <see cref="TValue:System.ComponentModel.PropertyDescriptor"></see> that represents the property whose owner is to be found. </param>
        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            if (TypeDescriptor.GetProperties(DeviceInfo).Contains(pd))
            {
                return DeviceInfo;
            }
            if (TypeDescriptor.GetProperties(programmer).Contains(pd))
            {
                return programmer;
            }
            if (TypeDescriptor.GetProperties(adapter).Contains(pd))
            {
                return adapter;
            }
            if (TypeDescriptor.GetProperties(hwInterface).Contains(pd))
            {
                return hwInterface;
            }
            if (TypeDescriptor.GetProperties(this, true).Contains(pd))
            {
                return this;
            }
            return null;
        }

        #endregion

        #region Private Members

        private void AddProperties(PropertyDescriptorCollection properties, 
            Object component, Attribute[] attributes, String category)
        {
            foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(component, attributes, false))
            {
                var attr = new Attribute[pd.Attributes.Count + 1];
                pd.Attributes.CopyTo(attr, 0);
                attr[attr.Length - 1] = new CategoryAttribute(category);
                properties.Add(new WorkspacePropertyDescriptor(pd, attr));
            }
        }

        #endregion

        #region WorkspacePropertyDescriptor

        private sealed class WorkspacePropertyDescriptor : PropertyDescriptor
        {
            public WorkspacePropertyDescriptor(PropertyDescriptor descr, Attribute[] attrs) : base(descr.Name, attrs)
            {
                this.descr = descr;
            }

            #region Overrides of PropertyDescriptor

            /// <summary>
            /// When overridden in a derived class, returns whether resetting an object changes its value.
            /// </summary>
            /// <returns>
            /// true if resetting the component changes its value; otherwise, false.
            /// </returns>
            /// <param name="component">The component to test for reset capability. </param>
            public override bool CanResetValue(object component)
            {
                return descr.CanResetValue(component);
            }

            /// <summary>
            /// When overridden in a derived class, gets the current value of the property on a component.
            /// </summary>
            /// <returns>
            /// The value of a property for a given component.
            /// </returns>
            /// <param name="component">The component with the property for which to retrieve the value. </param>
            public override object GetValue(object component)
            {
                return descr.GetValue(component);
            }

            /// <summary>
            /// When overridden in a derived class, resets the value for this property of the component to the default value.
            /// </summary>
            /// <param name="component">The component with the property value that is to be reset to the default value. </param>
            public override void ResetValue(object component)
            {
                descr.ResetValue(component);
            }

            /// <summary>
            /// When overridden in a derived class, sets the value of the component to a different value.
            /// </summary>
            /// <param name="component">The component with the property value that is to be set. </param>
            /// <param name="value">The new value. </param>
            public override void SetValue(object component, object value)
            {
                descr.SetValue(component, value);
            }

            /// <summary>
            /// When overridden in a derived class, determines a value indicating whether the value of this property needs to be persisted.
            /// </summary>
            /// <returns>
            /// true if the property should be persisted; otherwise, false.
            /// </returns>
            /// <param name="component">The component with the property to be examined for persistence. </param>
            public override bool ShouldSerializeValue(object component)
            {
                return descr.ShouldSerializeValue(component);
            }

            /// <summary>
            /// When overridden in a derived class, gets the type of the component this property is bound to.
            /// </summary>
            /// <returns>
            /// A <see cref="TValue:System.Type"></see> that represents the type of component this property is bound to. When the <see cref="M:System.ComponentModel.PropertyDescriptor.GetValue(System.Object)"></see> or <see cref="M:System.ComponentModel.PropertyDescriptor.SetValue(System.Object,System.Object)"></see> methods are invoked, the object specified might be an instance of this type.
            /// </returns>
            public override Type ComponentType
            {
                get
                {
                    return descr.ComponentType;
                }
            }

            /// <summary>
            /// When overridden in a derived class, gets a value indicating whether this property is read-only.
            /// </summary>
            /// <returns>
            /// true if the property is read-only; otherwise, false.
            /// </returns>
            public override bool IsReadOnly
            {
                get
                {
                    return descr.IsReadOnly;
                }
            }

            /// <summary>
            /// When overridden in a derived class, gets the type of the property.
            /// </summary>
            /// <returns>
            /// A <see cref="TValue:System.Type"></see> that represents the type of the property.
            /// </returns>
            public override Type PropertyType
            {
                get
                {
                    return descr.PropertyType;
                }
            }

            #endregion

            #region Object Overrides

            public override bool Equals(object obj)
            {
                var other = obj as WorkspacePropertyDescriptor;
                if (other != null)
                {
                    return descr.Equals(other.descr);
                }
                return false;
            }

            public override int GetHashCode()
            {
                return descr.GetHashCode();
            }

            #endregion

            #region Private Members

            private readonly PropertyDescriptor descr;

            #endregion
        }

        #endregion
    }*/
}
